package cn.virens.database.common.mapper;

public class ExampleJoin {
	private String condition;
	private String table;
	private String type;

	private ExampleJoin(String type, String table, String condition) {
		this.condition = condition;
		this.table = table;
		this.type = type;
	}

	public String getType() {
		return type;
	}

	public String getTable() {
		return table;
	}

	public String getCondition() {
		return condition;
	}

	public static ExampleJoin left(String table, String condition) {
		return new ExampleJoin("LEFT", table, condition);
	}

	public static ExampleJoin inner(String table, String condition) {
		return new ExampleJoin("INNER", table, condition);
	}

	/**
	 * 添加联表查询
	 * 
	 * @param  t1  权限表
	 * @param  t2  数据表
	 * @param  key 关联字段
	 * @return     ExampleJoin
	 */
	public static ExampleJoin inner(String t1, String t2, String key) {
		return inner(t1, t2, key, key);
	}

	/**
	 * 添加联表查询
	 * 
	 * @param  t1 权限表
	 * @param  t2 数据表
	 * @param  k1 权限关联字段
	 * @param  k2 数据关联字段
	 * @return    ExampleJoin
	 */
	public static ExampleJoin inner(String t1, String t2, String k1, String k2) {
		return inner(t1, t1 + ".`" + k1 + "` = " + t2 + ".`" + k2 + "`");
	}

	public static ExampleJoin of(String type, String table, String condition) {
		return new ExampleJoin(type, table, condition);
	}

	public ExampleJoin addConditionWithAnd(String condition) {
		if (this.condition != null && !this.condition.isEmpty()) {
			this.condition = (this.condition + " AND " + condition);
		} else {
			this.condition = condition;
		}

		return this;
	}

	public ExampleJoin addConditionWithOr(String condition) {
		if (this.condition != null && !this.condition.isEmpty()) {
			this.condition = (this.condition + " OR " + condition);
		} else {
			this.condition = condition;
		}

		return this;
	}
}
